#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::PayTypeBean;
use crate::domain::PayTypeQueryRequest;
use crate::serviceimpl::init_db;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use sqlx::Error;
use substring::Substring;

pub async fn activate_all_pay_types(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let pay_types = &app_state.await.context.pay_types;
	let vecter_iterator = ids.iter();
	for pay_type_id in vecter_iterator {
		let _ = pay_types.activate_pay_type(*pay_type_id).await;
	}
	Ok(0)
}

pub async fn activate_pay_type(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.activate_pay_type(id).await
}

pub async fn add_pay_type(pay_type:PayTypeBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.add_pay_type(pay_type).await
}

pub async fn clone_all_pay_types(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for pay_type_id in vecter_iterator {
		let _ = clone_pay_type(*pay_type_id).await;
	}
	Ok(0)
}

pub async fn clone_pay_type(id:i64) ->Result<u64, Error> {
	let mut pay_type = find_pay_type_by_id(id).await.unwrap();
	let name = find_pay_type_indexed_name(pay_type.name).await.unwrap();
	pay_type.name = name;
	add_pay_type(pay_type).await
}

pub async fn count_search_pay_types_by_fields_records(pay_type_query_request:PayTypeQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.count_search_pay_types_by_fields_records(pay_type_query_request).await
}

pub async fn delete_all_pay_types(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let pay_types = &app_state.await.context.pay_types;
	let vecter_iterator = ids.iter();
	for pay_type_id in vecter_iterator {
		let _ = pay_types.delete_pay_type(*pay_type_id).await;
	}
	Ok(0)
}

pub async fn delete_pay_type(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.delete_pay_type(id).await
}

pub async fn find_pay_type_by_id(id:i64) ->Result<PayTypeBean, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.find_pay_type_by_id(id).await
}

pub async fn find_pay_type_by_name(name:String) ->Result<PayTypeBean, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.find_pay_type_by_name(name).await
}

pub async fn list_active_pay_types() ->Result<Vec<PayTypeBean>, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.list_active_pay_types().await
}

pub async fn list_all_pay_types() ->Result<Vec<PayTypeBean>, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.list_all_pay_types().await
}

pub async fn search_pay_types_by_fields(pay_type_query_request:PayTypeQueryRequest) ->Result<Vec<PayTypeBean>, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.search_pay_types_by_fields(pay_type_query_request).await
}

pub async fn search_pay_types_by_fields_by_page(pay_type_query_request:PayTypeQueryRequest) ->Result<Vec<PayTypeBean>, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.search_pay_types_by_fields_by_page(pay_type_query_request).await
}

pub async fn soft_delete_all_pay_types(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let pay_types = &app_state.await.context.pay_types;
	let vecter_iterator = ids.iter();
	for pay_type_id in vecter_iterator {
		let _ = pay_types.soft_delete_pay_type(*pay_type_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_pay_type(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.soft_delete_pay_type(id).await
}

pub async fn toggle_one_pay_type(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let pay_types = &app_state.await.context.pay_types;
	let pay_type = pay_types.find_pay_type_by_id(id).await.unwrap();
	if pay_type.active == false {
		let _ = pay_types.toggle_pay_type(id).await;
	} else {
		let count = pay_types.count_active_pay_type_records().await.unwrap();
		if count.count_num > 1 {
			let _ = pay_types.toggle_pay_type(id).await;
		}
	}
	Ok(0)
}

pub async fn toggle_pay_type(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.toggle_pay_type(id).await
}

pub async fn update_pay_type(pay_type:PayTypeBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.pay_types.update_pay_type(pay_type).await
}

pub async fn find_pay_type_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let pay_types = app_state.await.context.pay_types.find_pay_type_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for pay_type in pay_types {
		let name2 = pay_type.name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

